import datetime
from core.tools.func_utils import geodistance


def distance_sorted(json_list, base_lon, base_lat, top_json_list=None):
    """
    排序咖啡机
    :param json_list:
    :param base_lon:
    :param base_lat:
    :param top_json_list: 置顶
    :return:
    """
    top_id_list = [i.get("id") for i in top_json_list]
    dis_list = []  # 距离容器
    gas_ids = []
    for i in json_list:
        dis = geodistance(float(base_lon), float(base_lat), i.get("lon"), i.get("lat"))
        dis_list.append(dis)
        gas_ids.append(i.get("id"))
    # 将加油站id 和需要排序的距离做zip
    gas_dis_dict = dict(zip(gas_ids, dis_list))
    # 将距离做排序
    dis_sort = sorted(dis_list)
    # 根据排序的价格找到gasid
    gas_dis_sort = gas_ids
    for gas, dis in gas_dis_dict.items():
        dis_index = dis_sort.index(float(dis))
        gas_dis_sort[dis_index] = gas  # 替换排序
    # 根据gasid排序加油站信息
    gas_info_list_sort = []
    for gas_id in gas_dis_sort:
        for gas_info in json_list:
            if gas_info.get("id") == gas_id and gas_info.get("id") not in top_id_list:
                gas_info_list_sort.append(gas_info)
                break
    top_json_list.extend(gas_info_list_sort)
    return top_json_list


def distance_sorted_v2(json_list, base_lon, base_lat, top_json_list=None):
    """
    排序咖啡机
    :param json_list:
    :param base_lon:
    :param base_lat:
    :param top_json_list: 置顶
    :return:
    """
    for i in json_list:
        dis = geodistance(float(base_lon), float(base_lat), i.get("lon"), i.get("lat"))
        i["dis"] = dis
    if top_json_list:
        for i in top_json_list:
            dis = geodistance(float(base_lon), float(base_lat), i.get("lon"), i.get("lat"))
            i["dis"] = dis
    else:
        top_json_list = []

    # 排序
    json_list = sorted(json_list, key=lambda x: x['dis'])
    top_json_list.extend(json_list)
    return top_json_list


def share_data():
    num = 3
    name = "立减券"
    channel = None
    expire_time = datetime.date.today() + datetime.timedelta(days=31)
    create_time = datetime.datetime.now()
    c_type = 2
    quota = 1500
    return num, name, channel, expire_time, create_time, c_type, quota